As with xs.c, change the module interface to match the current Python/C
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Mon, 21 Nov 2005 17:46:09 +0000 (18:46 +0100)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Mon, 21 Nov 2005 17:46:09 +0000 (18:46 +0100)
tutorial, simplifying the interface here, and making it more conventional.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/python/xen/lowlevel/xc/xc.c
tools/python/xen/xend/XendCheckpoint.py
tools/python/xen/xend/XendDmesg.py
tools/python/xen/xend/XendDomain.py
tools/python/xen/xend/XendDomainInfo.py
tools/python/xen/xend/XendNode.py
tools/python/xen/xend/image.py
tools/python/xen/xend/server/SrvDaemon.py
tools/python/xen/xend/server/iopif.py
tools/python/xen/xend/server/pciif.py
tools/python/xen/xm/create.py

index 669fc993c128619777d2ba4c9749643a95fbff6b..0b5f0b32a21dd7f4141e81109d44e165cc1ecd62 100644 (file)
@@ -23,7 +23,8 @@
 #define PyMODINIT_FUNC DL_EXPORT(void)
 #endif
 
-#define XENPKG "xen.lowlevel.xc"
+#define PKG "xen.lowlevel.xc"
+#define CLS "xc"
 
 static PyObject *xc_error, *zero;
 
@@ -1055,81 +1056,109 @@ static PyMethodDef pyxc_methods[] = {
 };
 
 
-/*
- * Definitions for the 'Xc' module wrapper.
- */
-
-staticforward PyTypeObject PyXcType;
+static PyObject *PyXc_getattr(PyObject *obj, char *name)
+{
+    return Py_FindMethod(pyxc_methods, obj, name);
+}
 
-static PyObject *PyXc_new(PyObject *self, PyObject *args)
+static PyObject *PyXc_new(PyTypeObject *type, PyObject *args)
 {
-    XcObject *xc;
+    XcObject *self = (XcObject *)type->tp_alloc(type, 0);
 
-    if ( !PyArg_ParseTuple(args, ":new") )
+    if (self == NULL)
         return NULL;
 
-    xc = PyObject_New(XcObject, &PyXcType);
-
-    if ( (xc->xc_handle = xc_interface_open()) == -1 )
-    {
-        PyObject_Del((PyObject *)xc);
-        return PyErr_SetFromErrno(xc_error);
-    }
+    self->xc_handle = NULL;
 
-    return (PyObject *)xc;
+    return (PyObject *)self;
 }
 
-static PyObject *PyXc_getattr(PyObject *obj, char *name)
+static int
+PyXc_init(XcObject *self, PyObject *args, PyObject *kwds)
 {
-    return Py_FindMethod(pyxc_methods, obj, name);
+    if ((self->xc_handle = xc_interface_open()) == -1) {
+        PyErr_SetFromErrno(PyExc_RuntimeError);
+        return -1;
+    }
+
+    return 0;
 }
 
-static void PyXc_dealloc(PyObject *self)
+static void PyXc_dealloc(XcObject *self)
 {
-    XcObject *xc = (XcObject *)self;
-    (void)xc_interface_close(xc->xc_handle);
-    PyObject_Del(self);
+    if (self->xc_handle) {
+        xc_interface_close(self->xc_handle);
+        self->xc_handle = NULL;
+    }
+
+    self->ob_type->tp_free((PyObject *)self);
 }
 
 static PyTypeObject PyXcType = {
-    PyObject_HEAD_INIT(&PyType_Type)
+    PyObject_HEAD_INIT(NULL)
     0,
-    "Xc",
+    PKG "." CLS,
     sizeof(XcObject),
     0,
-    PyXc_dealloc,    /* tp_dealloc     */
-    NULL,            /* tp_print       */
-    PyXc_getattr,    /* tp_getattr     */
-    NULL,            /* tp_setattr     */
-    NULL,            /* tp_compare     */
-    NULL,            /* tp_repr        */
-    NULL,            /* tp_as_number   */
-    NULL,            /* tp_as_sequence */
-    NULL,            /* tp_as_mapping  */
-    NULL             /* tp_hash        */
+    (destructor)PyXc_dealloc,     /* tp_dealloc        */
+    NULL,                         /* tp_print          */
+    PyXc_getattr,                 /* tp_getattr        */
+    NULL,                         /* tp_setattr        */
+    NULL,                         /* tp_compare        */
+    NULL,                         /* tp_repr           */
+    NULL,                         /* tp_as_number      */
+    NULL,                         /* tp_as_sequence    */
+    NULL,                         /* tp_as_mapping     */
+    NULL,                         /* tp_hash           */
+    NULL,                         /* tp_call           */
+    NULL,                         /* tp_str            */
+    NULL,                         /* tp_getattro       */
+    NULL,                         /* tp_setattro       */
+    NULL,                         /* tp_as_buffer      */
+    Py_TPFLAGS_DEFAULT,           /* tp_flags          */
+    "Xen client connections",     /* tp_doc            */
+    NULL,                         /* tp_traverse       */
+    NULL,                         /* tp_clear          */
+    NULL,                         /* tp_richcompare    */
+    0,                            /* tp_weaklistoffset */
+    NULL,                         /* tp_iter           */
+    NULL,                         /* tp_iternext       */
+    pyxc_methods,                 /* tp_methods        */
+    NULL,                         /* tp_members        */
+    NULL,                         /* tp_getset         */
+    NULL,                         /* tp_base           */
+    NULL,                         /* tp_dict           */
+    NULL,                         /* tp_descr_get      */
+    NULL,                         /* tp_descr_set      */
+    0,                            /* tp_dictoffset     */
+    (initproc)PyXc_init,          /* tp_init           */
+    NULL,                         /* tp_alloc          */
+    PyXc_new,                     /* tp_new            */
 };
 
-static PyMethodDef PyXc_methods[] = {
-    { "new", PyXc_new, METH_VARARGS, "Create a new " XENPKG " object." },
-    { NULL, NULL, 0, NULL }
-};
+static PyMethodDef xc_methods[] = { { NULL } };
 
 PyMODINIT_FUNC initxc(void)
 {
-    PyObject *m, *d;
+    PyObject *m;
 
-    m = Py_InitModule(XENPKG, PyXc_methods);
+    if (PyType_Ready(&PyXcType) < 0)
+        return;
 
-    d = PyModule_GetDict(m);
-    xc_error = PyErr_NewException(XENPKG ".error", NULL, NULL);
-    PyDict_SetItemString(d, "error", xc_error);
-    PyDict_SetItemString(d, "VIRQ_DOM_EXC", PyInt_FromLong(VIRQ_DOM_EXC));
+    m = Py_InitModule(PKG, PyXc_methods);
 
+    if (m == NULL)
+      return;
+
+    xc_error = PyErr_NewException(PKG ".error", NULL, NULL);
     zero = PyInt_FromLong(0);
 
     /* KAF: This ensures that we get debug output in a timely manner. */
     setbuf(stdout, NULL);
     setbuf(stderr, NULL);
+
+    Py_INCREF(&PyXcType);
+    PyModule_AddObject(m, CLS, (PyObject *)&PyXcType);
 }
 
 
index bfd59bcf7e4683fd94e03a343fd2124b9f6bd502..04fda474b49ccfbfef452ec25a33fca927f52a67 100644 (file)
@@ -33,7 +33,7 @@ sizeof_int = calcsize("i")
 sizeof_unsigned_long = calcsize("L")
 
 
-xc = xen.lowlevel.xc.new()
+xc = xen.lowlevel.xc.xc()
 
 
 def write_exact(fd, buf, errmsg):
index ab87befd3820e24006623a9c058553e8863d1157..a3bc8c45b4d86ea138eb1231c16d6fe6585da20a 100644 (file)
@@ -22,7 +22,7 @@ import xen.lowlevel.xc
 
 class XendDmesg:
     def __init__(self):
-        self.xc = xen.lowlevel.xc.new()
+        self.xc = xen.lowlevel.xc.xc()
 
     def info(self):
         return self.xc.readconsolering()
index 389e6114a36a973698087885c1385f7a6b1451be..9206c15b58500c259eb6c54fe2edf6065a4e5394 100644 (file)
@@ -39,7 +39,7 @@ from xen.xend.server import relocate
 from xen.xend.xenstore.xswatch import xswatch
 
 
-xc = xen.lowlevel.xc.new()
+xc = xen.lowlevel.xc.xc()
 xroot = XendRoot.instance()
 
 
@@ -201,7 +201,7 @@ class XendDomain:
                             "%d.  Destroying it in the hope of "
                             "recovery.", d)
                         try:
-                            xc.domain_destroy(dom = d)
+                            xc.domain_destroy(d)
                         except:
                             log.exception('Destruction of %d failed.', d)
 
@@ -378,7 +378,7 @@ class XendDomain:
             val = dominfo.destroy()
         else:
             try:
-                val = xc.domain_destroy(dom=domid)
+                val = xc.domain_destroy(domid)
             except Exception, ex:
                 raise XendError(str(ex))
         return val       
index c22d3e9abdb1db94962b94376b479afc1a321119..bbc7287f8206a2f55c8f68d0f40a2d90d42957ec 100644 (file)
@@ -93,7 +93,7 @@ MINIMUM_RESTART_TIME = 20
 RESTART_IN_PROGRESS = 'xend/restart_in_progress'
 
 
-xc = xen.lowlevel.xc.new()
+xc = xen.lowlevel.xc.xc()
 xroot = XendRoot.instance()
 
 log = logging.getLogger("xend.XendDomainInfo")
index fc0accd7d18aca631ab6352554163b4eefd825b2..250d11cecedcd63ecd6741014dfeece7ca5d1850 100644 (file)
@@ -28,7 +28,7 @@ import xen.lowlevel.xc
 class XendNode:
 
     def __init__(self):
-        self.xc = xen.lowlevel.xc.new()
+        self.xc = xen.lowlevel.xc.xc()
 
     def shutdown(self):
         return 0
@@ -40,7 +40,7 @@ class XendNode:
         return 0
     
     def cpu_bvt_slice_set(self, ctx_allow):
-        return self.xc.bvtsched_global_set(ctx_allow=ctx_allow)
+        return self.xc.bvtsched_global_set(ctx_allow)
 
     def cpu_bvt_slice_get(self):
         return self.xc.bvtsched_global_get()
index 77d07ea545a6188f5df8db13de95f796b3521990..0cc32ad97796b9c5b377e1254f5026a5ca64fab8 100644 (file)
@@ -27,7 +27,7 @@ from xen.xend.XendLogging import log
 from xen.xend.server.netif import randomMAC
 
 
-xc = xen.lowlevel.xc.new()
+xc = xen.lowlevel.xc.xc()
 
 
 MAX_GUEST_CMDLINE = 1024
index faa075767ae661551809f7452ca8435d506f7774..499da92f2268987a3c5ac97ed4f8c4666334741a 100644 (file)
@@ -267,7 +267,7 @@ class Daemon:
         try:
             log.info("Xend Daemon started")
 
-            xc = xen.lowlevel.xc.new()
+            xc = xen.lowlevel.xc.xc()
             xinfo = xc.xeninfo()
             log.info("Xend changeset: %s.", xinfo['xen_changeset'])
             del xc
index 25f05d01aff35e5b4a142838d8a4ef9050b92cbc..afeecc6d5cc917fd00f7c742cb8cd2186c0a3040 100644 (file)
@@ -28,7 +28,7 @@ from xen.xend.XendError import VmError
 from xen.xend.server.DevController import DevController
 
 
-xc = xen.lowlevel.xc.new()
+xc = xen.lowlevel.xc.xc()
 
 
 def parse_ioport(val):
index b98f6131340e1f80156b19f678ca7c4ce41d6903..7d7842e24d918b33378a0685d90e8e8520f5ab72 100644 (file)
@@ -27,7 +27,7 @@ from xen.xend.XendError import VmError
 from xen.xend.server.DevController import DevController
 
 
-xc = xen.lowlevel.xc.new()
+xc = xen.lowlevel.xc.xc()
 
 
 def parse_pci(val):
index 49a95ba277afdb399c1bc5bda475a5ee9cdf3eb1..6d90f85d0365e0bb984dc58f1816afc865f5bf2d 100644 (file)
@@ -849,7 +849,7 @@ def balloon_out(dom0_min_mem, opts):
     timeout = 20 # 2s
     ret = 1
 
-    xc = xen.lowlevel.xc.new()
+    xc = xen.lowlevel.xc.xc()
     free_mem = xc.physinfo()['free_pages'] / 256
     domU_need_mem = opts.vals.memory + SLACK